#include "spcontroller.h"
#include "mc_uiprotocol.h"

SPController::SPController(QObject *parent) : QObject(parent)
{
    mp_SPBuildingMapModel = NULL;
}
void SPController::getFloorDevice(int buildtype, QVariant buildindex)
{
    if(mp_SPBuildingMapModel == NULL){
        qDebug()<<FLINE<<"Error SP Building Model is NULL";
    }
    qDebug()<<FLINE<<"SP Usr Clicked "<<buildtype<<buildindex;
    mp_SPBuildingMapModel->userClickedDistrickItem(buildtype , buildindex);
}
void SPController::setSPQmlViewRootItem(const QObject *rootqmlview)
{
    mp_SPBuildingMapModel = new BuildingMapModel(rootqmlview->findChild<QObject*>("objspbuildview"));
}
void SPController::processData(QByteArray data)
{
    GUI_SHM_PACKET_HEADER *pGUIPacketHead = (GUI_SHM_PACKET_HEADER*)(data.data());
    GUI_SHM_LT_INFO *pLTInfo = (GUI_SHM_LT_INFO*)(data.data()+sizeof(GUI_SHM_PACKET_HEADER));
    GUI_SHM_SUB_DEV_INFO *pSPInfo = (GUI_SHM_SUB_DEV_INFO*)(data.data()+sizeof(GUI_SHM_SUB_DEV_INFO));

    if (pGUIPacketHead->devtype == DEVICE_SPEAKER) {
        QString spisn = QString(pSPInfo->subdevisn);
        int sdno = pSPInfo->subdevno;
        QString spevent = "";
        switch (pSPInfo->cmd) {
        case CMD_GUI_DEVICE_ON:
            spevent = "Online";
            break;
        case CMD_GUI_DEVICE_OFF:
            spevent = "Offline";
            break;
        case CMD_START_ALARM:
            spevent = "Emg";
            break;
        case CMD_CANCEL_ALARM:
            spevent = "CancelEmg";
            break;
        }
        spEvent(spisn , spevent , false);
    }
    else if(pGUIPacketHead->devtype == DEVICE_LT){
        QString ltisn = QString(pLTInfo->ltisn);
        QString ltevent = "";
        switch (pLTInfo->cmd) {
        case CMD_GUI_DEVICE_ON:
            ltevent = "Online";
            break;
        case CMD_GUI_DEVICE_OFF:
            ltevent = "Offline";
            break;
        }
        spEvent(ltisn , ltevent , true);
    }
    else {
        return;
    }
}
